/**
 * 文件名：ArrivalNoticeController.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：到货通知请求层
 */
package com.lightkits.wms.controller;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.arrivalnotice.model.TicketStructureDTO;
import com.lightkits.wms.domain.arrivalnotice.service.ArrivalNoticeService;
import com.lightkits.wms.dto.arrivalnotice.CancelArrivalNoticeRequestType;
import com.lightkits.wms.dto.arrivalnotice.CancelArrivalNoticeResponseType;
import com.lightkits.wms.dto.arrivalnotice.ExecuteArrivalNoticeRequestType;
import com.lightkits.wms.dto.arrivalnotice.ExecuteArrivalNoticeResponseType;
import com.lightkits.wms.dto.arrivalnotice.PrintArrivalNoticeRequestType;
import com.lightkits.wms.dto.arrivalnotice.PrintArrivalNoticeResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalInfoRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalInfoResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalNoticeLineListRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalNoticeLineListResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalNoticeListRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryArrivalNoticeListResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryNewArrivalLineRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryNewArrivalLineResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryNewSearchRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryNewSearchResponseType;
import com.lightkits.wms.dto.arrivalnotice.QueryReceiveInfoRequestType;
import com.lightkits.wms.dto.arrivalnotice.QueryReceiveInfoResponseType;
import com.lightkits.wms.dto.arrivalnotice.SaveArrivalNoticeRequestType;
import com.lightkits.wms.dto.arrivalnotice.SaveArrivalNoticeResponseType;
import com.lightkits.wms.dto.arrivalnotice.model.ArrivalNoticeServiceDTO;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author jie.ao@hand-china.com
 * @create 2019-07-03 15:28
 * @desc
 **/
@RestController
@RequestMapping("/ArrivalNoticeService")
public class ArrivalNoticeController {
    private final ArrivalNoticeService service;

    public ArrivalNoticeController(ArrivalNoticeService service) {
        this.service = service;
    }

    /**
     * 获取到货通知头信息
     * @param request 请求request
     * @return response
     */
    @PostMapping(value = "/queryArrivalNoticeList")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/queryArrivalNoticeList")
    @ResponseBody
    public QueryArrivalNoticeListResponseType queryArrivalNoticeList(@RequestBody
                                                                     QueryArrivalNoticeListRequestType request) {
        QueryArrivalNoticeListResponseType response = service.queryArrivalNoticeList(request.getTicketNum(),
                request.getPartyName(), request.getOrganizationName());
        if (ObjectUtil.isEmpty(response)) {
            response.setSuccessful(false);
            response.setMessage("Failed to get ArrivalNoticeList");
            return response;
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 获取到货通知行信息
     * @param request 请求的request
     * @return response
     */
    @PostMapping(value = "/queryArrivalNoticeLineList")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "查询到货单行")
    @ResponseBody
    public QueryArrivalNoticeLineListResponseType queryArrivalNoticeLineList(
            @RequestBody QueryArrivalNoticeLineListRequestType request) {
        QueryArrivalNoticeLineListResponseType response = service.queryArrivalNoticeLineList(request.getTicketId(),
                request.getPartyId());
        if (ObjectUtil.isEmpty(response)) {
            throw new LightKitsBizException("ArrivalNoticeLineList is not exist");
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 查询历史到货信息
     * @param request 传itemId
     * @return 历史到货信息DTO
     */
    @PostMapping(value = "/queryArrivalInfo")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "查询历史到货信息")
    @ResponseBody
    public QueryArrivalInfoResponseType queryArrivalInfo(@RequestBody QueryArrivalInfoRequestType request) {
        QueryArrivalInfoResponseType response = service.queryArrivalInfo(request.getItemId());
        if (ObjectUtil.isEmpty(response)) {
            throw new LightKitsBizException("arrivalInfoDTOList is not exist");
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 查询接收信息
     * @param request 传到货头id
     * @return 接收信息DTO
     */
    @PostMapping(value = "/queryReceiveInfo")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "查询接收货位信息")
    @ResponseBody
    public QueryReceiveInfoResponseType queryReceiveInfo(@RequestBody QueryReceiveInfoRequestType request) {
        QueryReceiveInfoResponseType response = service.queryReceiveInfo(request.getTickId());
        if (ObjectUtil.isEmpty(response)) {
            throw new LightKitsBizException("receiveInfoDTOList is not exist");
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 新增到货通知行信息LOV
     * @param request 前台传来的查询参数
     * @return 到货通知行信息
     */
    @PostMapping(value = "/queryNewArrivalLine")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "新增到货通知行信息")
    @ResponseBody
    public QueryNewArrivalLineResponseType queryNewArrivalLine(@RequestBody(required = false) QueryNewArrivalLineRequestType request) {
        QueryNewArrivalLineResponseType response = service.queryNewArrivalLine(request.getPoNum(),
                request.getItemCode(),
                request.getPlanTime(),
                request.getPlanTimeTo());
        if (ObjectUtil.isEmpty(response)) {
            throw new LightKitsBizException("newArrivalLineDTOList is not exist");
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 查询到货通知行中搜索字段
     * @param request 查询request
     * @return QueryNewSearchResponseType
     */
    @PostMapping(value = "/queryNewSearchInfo")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "到货通知行中搜索字段")
    @ResponseBody
    public QueryNewSearchResponseType queryNewSearchInfo(@RequestBody(required = false) QueryNewSearchRequestType request) {
        QueryNewSearchResponseType response = service.queryNewSearchInfo(request.getPartyId());
        if (ObjectUtil.isEmpty(response)) {
            throw new LightKitsBizException("newSearchDTOList is not exist");
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 保存到货通知头行
     * @param request request
     * @return response
     */
    @PostMapping(value = "/saveArrivalNotice")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "保存到货通知")
    @ResponseBody
    public SaveArrivalNoticeResponseType saveArrivalNotice(@RequestBody(required = false) SaveArrivalNoticeRequestType request) {
        SaveArrivalNoticeResponseType response = new SaveArrivalNoticeResponseType();
        ArrivalNoticeServiceDTO dto = request.getArrivalNoticeServiceDTO();
        TicketStructureDTO insertDTO = new TicketStructureDTO();
        TicketStructureDTO modifyDTO = new TicketStructureDTO();
        IResult result = service.saveArrivalNotice(dto, insertDTO, modifyDTO);
        if (!result.isSuccessful()) {
            response.setSuccessful(false);
            response.setMessage(result.getMessage());
            return response;
        }
        service.insertDate(insertDTO, modifyDTO);
        response.setSuccessful(true);
        return response;
    }

    /**
     * 执行到货通知接口
     * @param request 请求request
     * @return response
     */
    @PostMapping(value = "/executeArrivalNotice")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "执行到货通知")
    @ResponseBody
    public ExecuteArrivalNoticeResponseType executeArrivalNotice(@RequestBody(required = false) ExecuteArrivalNoticeRequestType request) {
        IResult result = service.executeArrivalNotice(request.getServiceDTO());
        ExecuteArrivalNoticeResponseType response = new ExecuteArrivalNoticeResponseType();
        if (!result.isSuccessful()) {
            response.setMessage(result.getMessage());
            response.setSuccessful(false);
            return response;
        }
        response.setSuccessful(true);
        return response;
    }

    /**
     * 取消到货通知按钮接口
     * @param request 请求参数
     * @return response
     */
    @PostMapping(value = "/cancelArrivalNotice")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "取消执行到货通知")
    @ResponseBody
    public CancelArrivalNoticeResponseType cancelArrivalNotice(@RequestBody(required = false) CancelArrivalNoticeRequestType request) {
        IResult result = service.cancelArrivalNotice(request.getCancelArrivalNoticeServiceDTO());
        CancelArrivalNoticeResponseType response = new CancelArrivalNoticeResponseType();
        if (!result.isSuccessful()) {
            response.setMessage(result.getMessage());
            response.setSuccessful(false);
            return response;
        }
        response.setSuccessful(true);
        return response;
    }


    /**
     * 打印导出的接口
     * @param request 请求参数
     * @return response
     */
    @PostMapping(value = "/printArrivalNotice")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "取消执行到货通知")
    @ResponseBody
    public PrintArrivalNoticeResponseType printArrivalNotice(@RequestBody(required = false)PrintArrivalNoticeRequestType request) {
        IResult result = service.printArrivalNotice(request.getPrintDTO());
        PrintArrivalNoticeResponseType response = new PrintArrivalNoticeResponseType();
        if (!result.isSuccessful()) {
            response.setMessage(result.getMessage());
            response.setSuccessful(false);
            return response;
        }
        response.setSuccessful(true);
        return response;
    }
}
